// show_modal/xt_show_modal.js
import show_modal from './appConfirm.js'
const xt_show_modal = {
	install: function(Vue) {
		const show_modal_fun = function(op = {}) {
			let vnode = null
			//#ifdef APP-PLUS
			return new Promise((resolve, reject) => {
				vnode = new show_modal({
					...op,
					$event: function(e) {
						if (e.res) {
							resolve(e);
						} else {
							reject(e);
						}
					}
				})
				vnode.show();
				uni.$showModalhide = function() {
					vnode.hide()
				}
				uni.$showModalshow = function() {
					vnode.show()
				}
				/* Vue.prototype.$hide = function() {
					vnode.hide()
				} */
			})
			//#endif

			// 适应H5
			//#ifdef H5
			var promise = uni.showModal({
				title: op.title,
				content: op.content,
				showCancel: !op.delCancel,
				cancelText: op.cancelVal,
				confirmText: op.confirmVal,
			});
			return new Promise((resolve, reject) => {
				promise.then(data => {
					var [err, res] = data;
					if (res.confirm) {
						resolve()
					} else {
						reject();
					}
				})
			})

			//#endif
			
		}
		// $showModal挂载到uni对象上
		uni.$showModal = show_modal_fun
		Vue.config.globalProperties.$showModal = show_modal_fun
		// Vue.prototype.$showModal = show_modal_fun
		
	}
};

export default xt_show_modal;